package com.dsc.ccrfid;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import app.ccrfid.com.serialportprotocollib.ProtocolManager;
import app.ccrfid.com.serialportprotocollib.callback.BindWSCallback;
import app.ccrfid.com.serialportprotocollib.callback.ScanWSCallback;
import app.ccrfid.com.serialportprotocollib.callback.SetFreeCallback;
import app.ccrfid.com.serialportprotocollib.callback.UnbindWSCallback;
import app.ccrfid.com.serialportprotocollib.callback.UpdateFrequencyCallback;
import app.ccrfid.com.serialportprotocollib.data.RangingData;
import app.ccrfid.com.serialportprotocollib.data.WSDevice;
import app.ccrfid.com.serialportprotocollib.exception.SPProtocolException;
import app.ccrfid.com.serialportprotocollib.util.datapack.RangingFrequency;
import app.ccrfid.com.serialportsdk.callback.OpenSerialPortCallback;

/**
 * This class echoes a string called from JavaScript.
 */
public class ccrfidplugin extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("add")) { // 测试用的。
            int arg0 = args.getInt(0);
            int arg1 = args.getInt(1);
            int sum = arg0 + arg1;
            callbackContext.success("{\"errcode\":0,\"sum\":" + sum + "}");
        } else if (action.equals("init")) {
            Context context = this.cordova.getActivity().getApplicationContext();
            ProtocolManager.getInstance().init(context);
            callbackContext.success("{\"errcode\":0,\"event\":\"success\"}");
        } else if (action.equals("openSerialPort")) {
            ProtocolManager.getInstance().openSerialPort(new OpenSerialPortCallback() {
                @Override
                public void onOpenSuccess() {
                    callbackContext.success("{\"errcode\":0,\"event\":\"success\"}");
                }

                @Override
                public void onOpenFail() {
                    callbackContext.error("{\"errcode\":-1,\"event\":\"fail\"}");
                }
            });
        } else if (action.equals("closeSerialPort")) {
            ProtocolManager.getInstance().closeSerialPort();
            callbackContext.success("{\"errcode\":0,\"event\":\"success\"}");
        } else if (action.equals("scanWSDevice")) {
            ProtocolManager.getInstance().scanWSDevice(new ScanWSCallback() {
                @Override
                public void onSendSuccess() {
                    String json = "{\"errcode\":0,\"event\":\"success\"}";
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json);
                    pluginResult.setKeepCallback(true);
                    callbackContext.sendPluginResult(pluginResult);
                }

                @Override
                public void onSendFail(SPProtocolException e) {
                    int code = e.getCode();
                    String des = e.getDescription();
                    String json = "{\"errcode\":" + code + ",\"errmsg\":\"" + des + "\"}";
                    callbackContext.error(json);
                }

                @Override
                public void onScanning(WSDevice wsDevice) {
                    String did = wsDevice.getWsDeviceId();
                    int ele = wsDevice.getWsDeviceElectricity();
                    int ver = wsDevice.getWsVersion();
                    String json = "{\"errcode\":0,\"event\":\"scan\",\"deviceid\":\"" + did + "\",\"electricity\":" + ele + ",\"version\":" + ver + "}";
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json);
                    pluginResult.setKeepCallback(true);
                    callbackContext.sendPluginResult(pluginResult);
                }
            });
        } else if (action.equals("bindWSDevice")) {
            String wsDeviceId = args.getString(0);
            ProtocolManager.getInstance().bindWSDevice(wsDeviceId, new BindWSCallback() {
                @Override
                public void onSendSuccess(String s) {
                    String json = "{\"errcode\":0,\"event\":\"success\",\"data\":\"" + s + "\"}";
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json);
                    pluginResult.setKeepCallback(true);
                    callbackContext.sendPluginResult(pluginResult);
                }

                @Override
                public void onSendFail(String s, SPProtocolException e) {
                    int code = e.getCode();
                    String des = e.getDescription();
                    String json = "{\"errcode\":" + code + ",\"errmsg\":\"" + des + "\",\"data\":\"" + s + "\"}";
                    callbackContext.error(json);
                }

                @Override
                public void onRanging(RangingData rangingData) {
                    float rang = rangingData.getRanging();
                    String did = rangingData.getWsDeviceId();
                    int electricity = rangingData.getWsDeviceElectricity();
                    String rid = rangingData.getReaderId();
                    String json = "{\"errcode\":0,\"event\":\"rang\",\"rang\":" + rang + ",\"deviceid\":\"" + did + "\",\"electricity\":" + electricity + ",\"readerid\":\"" + rid + "\"}";
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json);
                    pluginResult.setKeepCallback(true);
                    callbackContext.sendPluginResult(pluginResult);
                }
            });
        } else if (action.equals("unbindWSDevice")) {
            String wsDeviceId = args.getString(0);
            ProtocolManager.getInstance().unbindWSDevice(wsDeviceId, new UnbindWSCallback() {
                @Override
                public void onSendSuccess(String s) {
                    callbackContext.success("{\"errcode\":0,\"event\":\"success\",\"data\":\"" + s + "\"}");
                }

                @Override
                public void onSendFail(String s, SPProtocolException e) {
                    int code = e.getCode();
                    String des = e.getDescription();
                    String json = "{\"errcode\":" + code + ",\"errmsg\":\"" + des + "\",\"data\":\"" + s + "\"}";
                    callbackContext.error(json);
                }
            });
        } else if (action.equals("updateRangingFrequency")) {
            String wsDeviceId = args.getString(0);
            int frequency = args.getInt(1);
            RangingFrequency fre;
            switch (frequency) {
                case 1: fre = RangingFrequency.ONE;break;
                case 3: fre = RangingFrequency.THREE;break;
                case 4: fre = RangingFrequency.FOUR;break;
                case 5: fre = RangingFrequency.FIVE;break;
                default: fre = RangingFrequency.TWO;break;
            }
            ProtocolManager.getInstance().updateRangingFrequency(wsDeviceId, fre, new UpdateFrequencyCallback() {
                @Override
                public void onSendSuccess(String s) {
                    callbackContext.success("{\"errcode\":0,\"event\":\"success\",\"data\":\"" + s + "\"}");
                }

                @Override
                public void onSendFail(String s, SPProtocolException e) {
                    int code = e.getCode();
                    String des = e.getDescription();
                    String json = "{\"errcode\":" + code + ",\"errmsg\":\"" + des + "\",\"data\":\"" + s + "\"}";
                    callbackContext.error(json);
                }
            });
        } else if (action.equals("setReaderToFree")) {
            ProtocolManager.getInstance().setReaderToFree(new SetFreeCallback() {
                @Override
                public void onSendSuccess() {
                    callbackContext.success("{\"errcode\":0,\"event\":\"success\"}");
                }

                @Override
                public void onSendFail(SPProtocolException e) {
                    int code = e.getCode();
                    String des = e.getDescription();
                    String json = "{\"errcode\":" + code + ",\"errmsg\":\"" + des + "\"}";
                    callbackContext.error(json);
                }

                @Override
                public void onReceiveData(String s, String s2) {
                    callbackContext.success("{\"errcode\":0,\"event\":\"receivedata\",\"data\":\"" + s + "\",\"data2\":\"" + s2 + "\"}");
                }
            });
        } else if (action.equals("release")) {
            ProtocolManager.getInstance().release();
            callbackContext.success("{\"errcode\":0,\"event\":\"success\"}");
        }
        return true;
    }
}
